<template>
    <div class="usertx">
        <van-image
        round
        width="80"
        height="80"
        :src="user_img ? user_img : $ip + user.info.userImage"
        @click="xuanzshow = true"
        />
        
        <!-- 加载动画 -->
        <div class="load" v-show="load">
            <van-loading type="spinner" color="#fff"/>
        </div>

        <!-- 更换头像页面 -->
        <van-popup v-model="show" round position="bottom" :style="{ height: '70%' }">
            <van-uploader class="item" :after-read="afterRead"/>
            <div v-for="(url, index) in list" :key="index" class="item">
                <van-image
                width="80"
                height="80"
                :src="$ip + url"
                @click="onqiehuan(url)"
                />
            </div>
        </van-popup>
        
        <!-- 查看or更换头像面板 -->
        <van-action-sheet cancel-text="取消" v-model="xuanzshow" :actions="actions" @select="onSelect" />   <!--select	点击选项时触发，禁用或加载状态下不会触发-->
    </div>
</template>

<script>
import { headImageDefault } from "@/api/user";
import { mapState, mapMutations } from 'vuex'
export default {
    name : 'userTx',
    props : ['onBan', 'load'],      //来自login/index组件
    data() {
        return {
            show : false,
            xuanzshow : false,
            user_img : '',
            actions : [{ name: '查看头像' }, { name: '更换头像' }],
            list : [],
            file : '',
            isslog: false
        }
    },
    computed : {
        ...mapState(['user'])
    },
    methods : {
        ...mapMutations(['getUserInfo']),

        afterRead(file){
            this.show = false
            if(this.onBan != undefined){
                this.onBan('base64', file.content)
            }
            this.user_img = file.content
        },

        onqiehuan(url){
            if(this.onBan != undefined){
                this.onBan(url)
            }
            this.user_img = this.$ip + url
            this.show = false
        },

        // 获取推荐头像列表
        getimg(){
            headImageDefault().then(r => {
                if(r.code == 200){
                    this.list = r.data
                    if(this.user.info.userImage == undefined){
                        this.user_img = this.$ip + this.list[0]
                        if(this.onBan != undefined){
                            this.onBan(r.data[0])
                        }
                    }
                }
            })
        },

        // 处理查看、更换头像选项
        onSelect({ name }){
            this.xuanzshow = false
            if(name == '查看头像'){
                this.$ImagePreview({
                    images : [this.user_img ? this.user_img : this.$ip + this.user.info.userImage],
                    showIndex : false
                })
            }else if(name == '更换头像'){
                this.show = true
                this.getimg()
            }
        },
    },
    created(){
        this.getimg()
    },
}
</script>

<style scoped>
.usertx{
    position: relative;
    height: 80px;
    width: 80px;
}
.load{
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    z-index: 99;
    display: flex;
    justify-content: center;
    align-items: center;
}
.item{
    width: calc(33% - 20px);
    height: 80px;
    margin: 10px;
    float: left;
    line-height: 80px;
}
</style>